'use strict';



angular.module('myApp.hosts_comm', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/testing/hosts_comm', {
            templateUrl: 'pages/testing/hosts_comm/hosts_comm.html',
            controller: 'HostsCommCtrl'
        });
    }])

    .controller('HostsCommCtrl', function ($scope, $http, $location, $rootScope, $filter) {
        $rootScope.show_header = true
        $rootScope.show_footer = true
        $rootScope.title = '主机通信'
        console.log($location.path(), ' entered')
        $scope.host = $location.search().host || '192.168.50.225'
        let ele = document.getElementById('mobile-page');

        $rootScope.onNewMsg = (msg) => {
            if ($location.path() == '/testing/hosts_comm') {
                ele.scrollTop = ele.scrollHeight;
                if (msg.msg && msg.from != $scope.host && msg.from != 'localhost') {
                    swal.fire({
                        title: translate("新消息"),
                        text: `${msg.from}:\n${msg.msg}`,
                        // showConfirmButton: true,
                        showCancelButton: true,
                        cancelButtonText: "关闭",
                    });
                }

            }
        }

        const updateMessageList = async () => {
            // let arrs = await $rootScope.ros.getMsgHistory()
            // let msgs = arrs.map(item => JSON.parse(item))
            // console.log(msgs)
            // $scope.msgs = msgs
            ngRefresh()
            setTimeout(() => {
                ele.scrollTop = ele.scrollHeight;
                $http.get('/system/click_center')
            }, 100)

        }

        const kb = new SoftKeyboard('#soft-keyboard')

        kb.onComplete = async (msg) => {
            if (msg.length == 0) {
                return
            }
            console.log($scope.host, msg)
            const ele = document.querySelector('#input')
            ele.classList.add('hide')
            await $rootScope.ros.sendMsgTo($scope.host, msg)
            ngRefresh()
        }
        $rootScope.localHandler[$location.path()] = (e) => {
            console.log(e.code)
            if ($rootScope.show.length == 0) {
                return false
            }
            const ele = document.querySelector('#input')
            if (ele && !(ele.classList.contains('hide'))) {
                if (kb.keyHandler(e)) {
                    return true
                }
            }
            switch (e.code) {
                case "Enter":
                    if (ele && ele.classList.contains('hide')) {
                        // $rootScope.itemIndex
                        // let value = $rootScope.items[$rootScope.itemIndex].value
                        const title = document.querySelector('#input .title')
                        title.textContent = $filter('translate')('编辑')
                        const input = document.querySelector('#passwd')
                        input.value = ''
                        // input.focus()
                        ele.classList.remove('hide')
                    }
                    break;
                case "KeyB":
                    if (ele && !(ele.classList.contains('hide'))) {
                        ele.classList.add('hide')
                    } else {
                        return false
                    }
                    break;
                default:
                    return false
            }
            return true
        }
        updateMessageList()
    });

